﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data.MySqlClient;


using seo.dbutils;
using seo.Data;
using seo.DAO;


namespace seo.Buz
{
    public class ContentBuz
    {
        public string GetNativeUrlByKeyword(Website site, Keyword keyword)
        {
            string url = String.Empty;
            MySqlConnection conn = dbConnection.getLocalDbConnection();

            DbInfoBuz dbInfoBuz = new DbInfoBuz();
            DbInfo dbInfo = dbInfoBuz.getDbInfoByDomain(site.domainname);
            MySqlConnection remoteConn = dbConnection.getRemoteDbConnection(dbInfo);
            try
            {
                remoteConn.Open();
                ContentDao dao = new ContentDao(remoteConn);
                url = site.domainname + dao.GetNativeUrlByKeyword(keyword.keywordchinese, dbInfo.prefix);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                remoteConn.Close();
            }
            return url;
        }

        public int insertContent(Website site, Keyword keyword, string article, string title) 
        {
            int insertCount = 0;

            MySqlConnection conn = dbConnection.getLocalDbConnection();

            DbInfoBuz dbInfoBuz = new DbInfoBuz();
            DbInfo dbInfo = dbInfoBuz.getDbInfoByDomain(site.domainname);
            MySqlConnection remoteConn = dbConnection.getRemoteDbConnection(dbInfo);
            remoteConn.Open();
            MySqlTransaction trans = remoteConn.BeginTransaction();
            try
            {

                ContentDao contentDao = new ContentDao(remoteConn);

                int id = contentDao.getMaxId(dbInfo.prefix);

                string[] idStr = contentDao.getSectIdAndCatId(keyword.keywordchinese, dbInfo.prefix);

                DateTime now = DateTime.Now;

                Content content = new Content();

                content.id = id;
                content.title = title;
                content.alias = now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second;
                content.title_alias = string.Empty;
                content.introtext = "<p>" + article + "</p>";
                content.fulltext = string.Empty;
                content.state = 1;
                content.sectionid = idStr[0];
                content.mask = 0;
                content.catid = idStr[1];
                content.created = now;
                content.created_by = 62;
                content.created_by_alias = string.Empty;
                //content.modified = DateTime.MinValue;
                content.modified_by = 0;
                content.checked_out = 0;
                //content.checked_out_time = "0000-00-00 00:00:00";

                Random r = new Random((int)DateTime.Now.Ticks);
                int day = r.Next(0, 8);
                int hour = r.Next(8, 19);

                DateTime publish_up = new DateTime(now.Year,now.Month,now.Day,hour,now.Minute,now.Second);
                publish_up = publish_up.AddDays(day);

                content.publish_up = publish_up;

                content.images = string.Empty;
                content.urls = string.Empty;
                content.attribs = "show_title=\r\nlink_titles=\r\nshow_intro=\r\nshow_section=\r\nlink_section=\r\nshow_category=\r\nlink_category=\r\nshow_vote=\r\nshow_author=0\r\nshow_create_date=0\r\nshow_modify_date=0\r\nshow_pdf_icon=\r\nshow_print_icon=\r\nshow_email_icon=\r\nlanguage=\r\nkeyref=\r\nreadmore=";
                content.version = 1;
                content.parentid = 0;
                content.ordering = 1;
                content.metakey = string.Empty;
                content.metadesc = string.Empty;
                content.access = 0;
                content.hits = 1;
                content.metadata = "robots=author=";

                contentDao.insertContent(content, dbInfo.prefix);

                trans.Commit ();

            }
            catch(Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
                remoteConn.Close();
            }

            return insertCount;
        }


    }
}
